/*----------------------------------------------
| noka checks and radios v1.0 www.97521.com    |
| rebin 2014-07-13                             |
|---------------------------------------------*/
var nkrchecks = Class.create({
    version: '1.0',
    initialize : function(cfg) {
    	this.id=cfg.id; //id
    	this.cfg = cfg;
    	this.name = cfg.name;
    	this.json=cfg.json || [];//[{value=fds,label=fds,name=abc},{value=fds,label=fds,name=abc}]
    	this.value = cfg.value || [];//[1,2]
    	this.obj = $(cfg.id);
    	this.readonly = ('readonly'==cfg.readonly);
    	this.stopcheck = cfg.stopcheck;//单个选项禁用
    	this.selectsun = cfg.selectsun;//为true表示可以为空
    	this.style = cfg.style;
    	this.classstyle = cfg.classstyle;
    	this.ctype = cfg.ctype;//checkbox | radio
    	this.stype = cfg.stype;//left|right
    	this.onclick=cfg.onclick;
    	this.width = cfg.width;
    	this.disabled = cfg.disabled;
    	this.rurl = cfg.rurl;
    	this.divstyle = cfg.divstyle;
    	this.divclassstyle = cfg.divclassstyle;
    },
    show : function(){
    	this.initselect();
    	this.ExEvents();
    },
   //------------------------------------------------------
    initselect : function(){
    	var idx = 0;
        var html = [];
        var self = this;
        var jon = self.json;
        html[idx++]='<div id="'+self.id+'_input" style="display: none;">';
        var bwidth = self.width / jon.length;
        var iwidth = bwidth+'px';
        if(self.classstyle==undefined || self.classstyle==''){
        	self.classstyle = 'nk_input_lineheight nk_checks';
        }
        for(var i=0;i<jon.length;i++){
        	if (typeof jon[i].name == "undefined"){
        		jon[i].name=self.name;
        	}
        	if (typeof jon[i].id == "undefined"){
        		jon[i].id=self.name+'_'+jon[i].value;
        	}
        	html[idx++] = '<input type="'+self.ctype+'" value="'+jon[i].value+'" name="'+jon[i].name+'" id="'+jon[i].id+'" />';
        }
        html[idx++]='</div>';
        if('disabled'==self.disabled){
        	for(var i=0;i<jon.length;i++){
        		if (typeof jon[i].width != "undefined"){
            		iwidth = jon[i].width;
            	}else{
            		iwidth = bwidth+'px';
            	}
                html[idx++] = '<input type="button" style="width:'+iwidth+'" disabled="disabled" id="'+jon[i].id+'_div" class="'+self.classstyle+'" value="'+jon[i].label+'"></input>';
            }
        }else if(self.readonly){
        	for(var i=0;i<jon.length;i++){
        		if (typeof jon[i].width != "undefined"){
            		iwidth = jon[i].width;
            	}else{
            		iwidth = bwidth+'px';
            	}
                html[idx++] = '<input type="button" style="width:'+iwidth+'" readonly="readonly" id="'+jon[i].id+'_div" class="'+self.classstyle+'" value="'+jon[i].label+'"></input>';
            }
        }else{
        	for(var i=0;i<jon.length;i++){
        		if (typeof jon[i].width != "undefined"){
            		iwidth = jon[i].width;
            	}else{
            		iwidth = bwidth+'px';
            	}
        		html[idx++] = '<input type="button" style="width:'+iwidth+'" id="'+jon[i].id+'_div" class="'+self.classstyle+'" value="'+jon[i].label+'"></input>';
        	}
        }
        if(self.selectsun>0){
            html[idx++] = '<div style="float:left;color: red;" class="nk_input_lineheight">*</div>';
            html[idx++] = '<div style="float:left;"><img style="display:none;vertical-align:middle" class="nk_input_imgheight" id="'+self.id+'_img" src="'+self.rurl+'/nokatag/formcheck/images/exclamation.gif"></img></div>';
        }
        self.obj.innerHTML=html.join('');
        self.initSelectValue();
       // if(!self.readonly && 'disabled'!=self.disabled){//如果不是只读
	        for(var i=0;i<jon.length;i++){
	        	$(jon[i].id+'_div').observe('click', function() {
	        		if(!self.readonly && 'disabled'!=self.disabled){
		        		var cid = this.id.substring(0,this.id.length-'_div'.length);
		        		try{
			        		if(undefined != self.stopcheck){
			        			var cidv = $(cid).value;
			        			if((','+self.stopcheck+',').indexOf(','+cidv+',')!=-1){
			        				return false;
			        			}
			        		}
		        		}catch(e){}
		        		if(!self.onclick($(cid))){//外部事件
		        				this.checked=!this.checked;
		        		}
		        		if('radio'==self.ctype){
		        			$(cid).checked = true;
		        		}else{
		        			$(cid).checked = !$(cid).checked;
		        		}
		        		self.initObjCht();
		        		for(var a=0;a<jon.length;a++){
		        			self.selectStyle($(jon[a].id+'_div'),$(jon[a].id).checked);
		        		}
	        		}
	        	});
	        	//------------鼠标移入高亮事件--------------------------------------
	        	$(jon[i].id+'_div').observe('mousemove', function() {
	        		if(!self.readonly && 'disabled'!=self.disabled){
	        			self.selectStyle(this,true);
	        		}
	        	});
	        	//------------鼠标移除事件----------------------------------------
	        	$(jon[i].id+'_div').observe('mouseout', function() {
	        		if(!self.readonly && 'disabled'!=self.disabled){
	        			var cid = this.id.substring(0,this.id.length-'_div'.length);
	        			self.selectStyle(this,$(cid).checked);
	        		}
	        	});
	        	
	        }
        //}
    },
    //------------初始化选择---------------------
    initSelectValue : function(){
    	var self = this;
        var jon = self.json;
        var va = self.value;
        for(var i=0;i<jon.length;i++){
    		self.selectStyle($(jon[i].id+'_div'),false);
    		$(jon[i].id).checked=false;
        }
    	for(var i=0;i<jon.length;i++){
        	for(var a=0;a<va.length;a++){
        		if(jon[i].value==va[a]){
        			$(jon[i].id).checked=true;
        			self.selectStyle($(jon[i].id+'_div'),true);
        			if('radio'==self.ctype){
        				return;
        			}
        		}
        	}
        }
    },
    //------------选中状态-----------------------
    selectStyle : function(obj,se){
    	if(se){//选种状态
    		//obj.setStyle({backgroundColor:'#428bca',borderColor:'#cccccc'});
    		obj.removeClassName('nk_checks');
    		obj.addClassName('nk_checks_checked');
    	}else{
    		obj.removeClassName('nk_checks_checked');
    		obj.addClassName('nk_checks');
    	}
    },
    //--------初始化验证-----------------
    initObjCht : function(){
    	var self = this;
    	var jon = self.json;
    	var sun = 0;
  		for(var i=0;i<jon.length;i++){
  			 if($(jon[i].id).checked){
  				 sun++;
  			 }
  		}
  		if(sun<self.selectsun){
  			 $(self.id+'_img').show();
  			 return false;
  		}
  		try{$(self.id+'_img').hide();}catch(e){}
  		return true;
    },
   //-----------外部事件--------------------------------
    ExEvents : function(){
    	var self = this;
        var jon = self.json;
        var va = self.value;
    	//-----------重新设备控件-----------------------
        self.obj.setJson = function(json){
    		self.json = json;
    		self.show();
    	};
    	//-----------设置选择的值-----------------------
    	self.obj.setValue = function(value){
    		if(Object.isString(value)){
    			value = "['"+value.replace(/\,/g,"','")+"']";
    			self.value = value.evalJSON();
    		}else{
    			self.value = value;
    		}
    		self.initSelectValue();
    		self.initObjCht();
    	};
    	//-----------获取值----------------------------
    	self.obj.getValue = function(){
    		var vc = [];
    		for(var i=0;i<jon.length;i++){
    			if($(jon[i].id).checked)
    				vc.push($(jon[i].id).value);
    		}
    		self.value = vc;
    		va = vc;
    		return self.value;
    	};
    	//-----------验证值----------------------------
    	self.obj.veri = function(){
    		return self.initObjCht();
    	};
    	//-----------验证-----------------------------
    	for(var i=0;i<jon.length;i++){
    		$(jon[i].id).veri = function(){
    			return self.initObjCht();
    		};
    	}
    	//----------------------------------
    	self.obj.setDisabled = function(a){
    		for(var i=0;i<jon.length;i++){
    			$(jon[i].id+'_div').writeAttribute({disabled:a});
            }
    		self.disabled=(a?'disabled':'');
    	};
    	self.obj.setReadonly = function(a){
    		for(var i=0;i<jon.length;i++){
    			$(jon[i].id+'_div').writeAttribute({readonly:a});
            }
    		self.readonly=a;
    	};
    	//---------------------------------------------
    	document.observe('widget:noka_reinit',function(event){//失失焦点时验证其值
   		 if(self.id.indexOf('_nkADinsert_')==-1){
	    		var npfffs=event.memo.widgetNumber.clone();
	    		for(var i=0;i<npfffs.length;i++){
	    			var initem = npfffs[i];
	    			if(initem.id.indexOf('_'+self.id+'_')!=-1){
	    				var ckcfgid = Object.allclone(self.cfg);
		    			ckcfgid.id = initem.id;
		    			if(undefined!=initem.value){
		    				ckcfgid.value = initem.value;
		    			}
	    				for(var c=0;c<ckcfgid.json.length;c++){
		    				ckcfgid.json[c].id=self.cfg.json[c].id+'_'+initem.id;
		    			}
	    				(new nkrchecks(ckcfgid)).show();
	    				try{
	    					for(var c=0;c<ckcfgid.json.length;c++){
			    				var cid = ckcfgid.json[c].id;
			    				var idx = cid.substring(cid.lastIndexOf('_')+1,cid.length);
		    					$(cid).name=$(cid).name.replace(/\[0\]/g,'['+idx+']');
			    			}
	    				}catch(e){}
	    			}
	    		}
   		 }
   	   });
      //-------------------------------------------------------
    }
});